/* (C) Copyright 2024, ZWSOFT Co., LTD.(Guangzhou) All Rights Reserved. */
/** @file Function.hpp
 *  @brief 解析器数据结构
 *  @details 用于存储解析器所需要的各种数据
 */
#pragma once
#include "DataStruct.hpp"


 /**
  * @brief 函数类
  * @details 储存：函数名，函数开始的行号，参数列表及其值，函数返回值；封装了代码解析器
  * @return true:代码正常执行，false:文件读取失败
  */
class Function {
public:
	Function() : m_out(Variable()) {};
	Function(const std::string& funcTitle);
	Function(const std::vector<std::string>& code_, const size_t start, const size_t end);

	std::string run(const std::vector<std::string>& psStr, std::unordered_map<std::string, Function>& funcs);

	std::string get_name() const { return m_name; }
	std::string get_full_name() const { return m_fullName; }
	void set_funcs_ptrs(std::unordered_map<std::string, Function>* funcs, std::unordered_set<std::string>* names)
	{
		m_funcs = funcs; 
		m_funcNames = names;
	}

private:
	Variable function_run(Params& ps);

	Variable set_func_parameters(const std::string& expr, Params& ps_input);

	Variable evaluate_exp(std::string& right_text,  Params& ps);

	void line_split(const std::string& codeLine, bool isNew, const bool isConst, const Type type, std::vector<std::string>& blockList, Params& ps);
	int equal_split(const std::string blockCode, std::string& leftText, std::string& rightText, Params& ps);
	bool experation_split(std::string& expr, std::vector<std::string>& varList, std::string& ops);
	void set_func_info(const std::string& func_title);
	int get_str_type(std::string typeName, Params& ps, bool& isConst, Type& type, std::stack<char>& ptr_ref, std::string& pureName);
	int set_definition(std::string& expr, bool& isConst, Type& type);
	int get_str_type_r(std::string namePre, Params& ps, std::stack<char>& ptr_ref, std::string& pureName);
	Variable str_to_var(std::string varInfo, Params& ps, std::unordered_map<std::string, Variable>& midVars);

	std::string m_name;
	std::string m_fullName;
	bool m_isDefined = false;
	std::vector<std::string> m_code;	// 函数代码
	std::vector<Variable> m_inputs;		// 参数列表，仅用于记录参数属性，不进行赋值运算
	Variable m_out;						// 返回值，仅用于记录参数属性，不进行赋值运算

	std::unordered_map<std::string, Function>* m_funcs = nullptr;
	std::unordered_set<std::string>* m_funcNames = nullptr;

	std::unordered_map<Type, std::string> tpName = {	// 仅用于输出查看，不参与运算
	{NONE, "none"},
	{INT, "int"},
	{LONG, "long"},
	{CHAR, "char"},
	{DOUBLE, "double"},
	{BOOL, "bool"},
	{VOID, "void"}
	};
};
